﻿using System;
using System.ComponentModel;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MyLib.Web.UI {
	/// <summary>
	/// 
	/// </summary>
	[ToolboxData("<{0}:MyDateTimeValidator runat=\"server\"></{0}:MyDateTimeValidator>")]
	public class MyDateTimeValidator : CustomValidator {

		/// <summary>
		/// Gruop1 - Year, Group2 - Month, Group5 - Day.
		/// </summary>
		private static readonly Regex DatePattern = new Regex(
@"^\s*(\d\d\d\d)\s*\-\s*((0|1)?\d)(\s*\-\s*((0|1|2|3)?\d))?\s*$",
RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline);

		/// <summary>
		/// Group1 - Year, Group2 - Month, Group5 - Day, Group 8 - Hour, Gruop 10 - Minute.
		/// </summary>
		private static readonly Regex DateTimePattern = new Regex(
@"^\s*(\d\d\d\d)\s*\-\s*((0|1)?\d)(\s*\-\s*((0|1|2|3)?\d))?\s*(((0|1|2)?\d)\s*:\s*((0|1|2|3|4|5)?\d))?\s*$",
RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline);

		/// <summary>
		/// Group1 - Hour, Group3 - Minute.
		/// </summary>
		private static readonly Regex TimePattern = new Regex(
@"^\s*((0|1|2)?\d)\s*:\s*((0|1|2|3|4|5)?\d)\s*$",
RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline);

		private bool m_initialized = false;

		/// <summary>
		/// 
		/// </summary>
		protected virtual void UpdateErrorMessage() {
			if (m_initialized) {
				ErrorMessage = string.Format(Properties.Main.ErrorInvalidFormat, PropertyTitle);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		[Category("Appearance")]
		[Bindable(true)]
		[Browsable(true)]
		[DefaultValue("")]
		public string PropertyTitle {
			get {
				var obj = ViewState["PropertyTitle"];

				return obj == null ? "" : (string)obj;
			}
			set {
				ViewState["PropertyTitle"] = value;

				UpdateErrorMessage();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		[Category("Appearance")]
		[Bindable(false)]
		[Browsable(true)]
		[DefaultValue(DateTimeMode.Date)]
		public DateTimeMode DateTimeMode {
			get {
				object obj = this.ViewState["DateTimeMode"];
				if (obj != null) {
					return (DateTimeMode)obj;
				}
				return DateTimeMode.Date;
			}
			set {
				this.ViewState["DateTimeMode"] = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="e"></param>
		protected override void OnInit(EventArgs e) {
			base.OnInit(e);

			Display = ValidatorDisplay.Dynamic;
			SetFocusOnError = true;

			if (string.IsNullOrWhiteSpace(ClientValidationFunction)) {
				ClientValidationFunction = DateTimeMode == DateTimeMode.Date ? @"my_dateTimeValidatorEvaluateIsValid1" :
					DateTimeMode == DateTimeMode.DateTime ? @"my_dateTimeValidatorEvaluateIsValid2" :
					DateTimeMode == DateTimeMode.Time ? @"my_dateTimeValidatorEvaluateIsValid3" : "";
			}

			m_initialized = true;
			if (string.IsNullOrWhiteSpace(ErrorMessage)) {
				UpdateErrorMessage();
			}
		}
	} // end of MyDateTimeValidator.

	/// <summary>
	/// 
	/// </summary>
	public enum DateTimeMode {
		/// <summary>
		/// Date.
		/// </summary>
		Date = 0,

		/// <summary>
		/// Date and time.
		/// </summary>
		DateTime,

		/// <summary>
		/// Time.
		/// </summary>
		Time
	} // end of DateTimeMode.
}
